package org.jeecg.modules.product.risk.service.impl;

import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.product.risk.entity.TbBaseDangerInvestigation;
import org.jeecg.modules.product.risk.entity.TbBaseDangerInvestigationReocrd;
import org.jeecg.modules.product.risk.mapper.TbBaseDangerInvestigationMapper;
import org.jeecg.modules.product.risk.mapper.TbBaseDangerInvestigationReocrdMapper;
import org.jeecg.modules.product.risk.service.ITbBaseDangerInvestigationService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: 隐患排查信息
 * @Author: jeecg-boot
 * @Date:   2024-05-07
 * @Version: V1.0
 */
@Service
public class TbBaseDangerInvestigationServiceImpl extends ServiceImpl<TbBaseDangerInvestigationMapper, TbBaseDangerInvestigation> implements ITbBaseDangerInvestigationService {

	@Autowired
	private TbBaseDangerInvestigationMapper tbBaseDangerInvestigationMapper;
	@Autowired
	private TbBaseDangerInvestigationReocrdMapper tbBaseDangerInvestigationReocrdMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(TbBaseDangerInvestigation tbBaseDangerInvestigation, List<TbBaseDangerInvestigationReocrd> tbBaseDangerInvestigationReocrdList) {
		tbBaseDangerInvestigation.setId(UUIDGenerator.generate());
		tbBaseDangerInvestigationMapper.insert(tbBaseDangerInvestigation);
		if(tbBaseDangerInvestigationReocrdList!=null && tbBaseDangerInvestigationReocrdList.size()>0) {
			for(TbBaseDangerInvestigationReocrd entity:tbBaseDangerInvestigationReocrdList) {
				//外键设置
				entity.setInvestigationId(tbBaseDangerInvestigation.getId());
				tbBaseDangerInvestigationReocrdMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(TbBaseDangerInvestigation tbBaseDangerInvestigation,List<TbBaseDangerInvestigationReocrd> tbBaseDangerInvestigationReocrdList) {
		tbBaseDangerInvestigationMapper.updateById(tbBaseDangerInvestigation);
		
		//1.先删除子表数据
		tbBaseDangerInvestigationReocrdMapper.deleteByMainId(tbBaseDangerInvestigation.getId());
		
		//2.子表数据重新插入
		if(tbBaseDangerInvestigationReocrdList!=null && tbBaseDangerInvestigationReocrdList.size()>0) {
			for(TbBaseDangerInvestigationReocrd entity:tbBaseDangerInvestigationReocrdList) {
				//外键设置
				entity.setInvestigationId(tbBaseDangerInvestigation.getId());
				tbBaseDangerInvestigationReocrdMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		tbBaseDangerInvestigationReocrdMapper.deleteByMainId(id);
		tbBaseDangerInvestigationMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			tbBaseDangerInvestigationReocrdMapper.deleteByMainId(id.toString());
			tbBaseDangerInvestigationMapper.deleteById(id);
		}
	}

	@Override
	public List<Map> countForThisYearByMonth() {
		return tbBaseDangerInvestigationMapper.countForThisYearByMonth();
	}
}
